<template>
  <a-modal :title="title" :after-close="reset" @ok="submit">
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ style: { width: '80px' } }">
      <a-form-item label="数据组名称" name="name">
        <a-input v-model:value="form.name" placeholder="数据组名称" />
      </a-form-item>
      <a-form-item label="简介" name="info">
        <a-input v-model:value="form.info" placeholder="简介" />
      </a-form-item>
      <a-form-item label="表单数据ID" name="formId">
        <a-input v-model:value="form.formId" placeholder="form 表单 id" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup lang="ts">
import { systemgroupAddup, systemgroupinfo } from './api';
import { systemgroup, type Isystemgroup } from './entity';
import { validReg } from '@/utils/validate';

const props = withDefaults(defineProps<{
  data: Partial<Isystemgroup>
}>(), {
})
const title = computed(() => props.data.id ? '编辑' : '新增')
const { formRef, submit, close } = useDialog({ validCallback: save })

let form = $ref(new systemgroup())
watchEffect(() => {
  if (props.data.id) {
    systemgroupinfo(props.data.id).then(res => {
      form.setProps(res)
    })
  }
})

const rules = {
  systemgroupName: [validReg('required', '请输入用户名')]
}

onMounted(() => {
})
const emit = defineEmits(['change'])
function save() {
  systemgroupAddup(form).then(() => {
    AntMessage.success('保存成功')
    close()
    emit('change')
  })
}

function reset() {
  form = new systemgroup()
}
</script>

<style lang="less"></style>